unit uFrmVerificarSequencial;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, JvComponentBase, JvEnterTab, Menus, DB, MemDS,
  DBAccess, Uni;

type
  TFrmVerificaSequencial = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Memo1: TMemo;
    Label3: TLabel;
    Label4: TLabel;
    DateTimePicker1: TDateTimePicker;
    DateTimePicker2: TDateTimePicker;
    EdtSerie: TEdit;
    EdtCodTMV: TEdit;
    Label5: TLabel;
    BtnAdicionar: TButton;
    BtnVerificar: TButton;
    BtnCarregarUltimasConfigs: TButton;
    Memo2: TMemo;
    JvEnterAsTab1: TJvEnterAsTab;
    PopupMenu1: TPopupMenu;
    Limpar1: TMenuItem;
    UniQuery1: TUniQuery;
    procedure FormCreate(Sender: TObject);
    procedure BtnAdicionarClick(Sender: TObject);
    procedure JvEnterAsTab1HandleEnter(Sender: TObject; AControl: TWinControl;
      var Handled: Boolean);
    procedure Limpar1Click(Sender: TObject);
    procedure BtnVerificarClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FrmVerificaSequencial: TFrmVerificaSequencial;
  vMovs : TStringList;

implementation

uses uDM;

{$R *.dfm}

procedure TFrmVerificaSequencial.BtnAdicionarClick(Sender: TObject);
begin
  if (EdtSerie.Text <> '') and (EdtCodTMV.Text <> '') then
  begin
    vMovs.Add(EdtSerie.Text + ',' + EdtCodTMV.Text + ',' + DateToStr(DateTimePicker1.Date) + ',' + DateToStr(DateTimePicker2.Date));
    Memo1.Lines.Text := vMovs.Text;
    EdtSerie.SetFocus;
  end;
end;

procedure TFrmVerificaSequencial.BtnVerificarClick(Sender: TObject);
var i , i2 , min , max : Integer;
  vMovTemp : TStringList;
begin
  vMovTemp := TStringList.Create;
  vMovTemp.StrictDelimiter := true;
  vMovTemp.DelimitedText := ',';
  for i := 0 to vMovs.Count - 1 do
  begin
    vMovTemp.Clear;
    vMovTemp.DelimitedText := vMovs[i];
    UniQuery1.Close;
    UniQuery1.SQL.Text := 'select min(numeromov) from tmov where (dataemissao between ' +
    QuotedStr(StringReplace(vMovTemp[2],'/','.',[rfReplaceAll,rfIgnoreCase])) + ' and ' + QuotedStr(StringReplace(vMovTemp[3],'/','.',[rfReplaceAll,rfIgnoreCase])) + ') and (codtmv = ' +
    QuotedStr(vMovTemp[1]) + ') and (serie = ' + QuotedStr(vMovTemp[0]) + ')';
    //ShowMessage(UniQuery1.SQL.Text);
    UniQuery1.Open;
    min := UniQuery1.FieldByName('min').AsInteger;

    UniQuery1.Close;
    UniQuery1.SQL.Text := 'select max(numeromov) from tmov where (dataemissao between ' +
    QuotedStr(StringReplace(vMovTemp[2],'/','.',[rfReplaceAll,rfIgnoreCase])) + ' and ' + QuotedStr(StringReplace(vMovTemp[3],'/','.',[rfReplaceAll,rfIgnoreCase])) + ') and (codtmv = ' +
    QuotedStr(vMovTemp[1]) + ') and (serie = ' + QuotedStr(vMovTemp[0]) + ')';
    //ShowMessage(UniQuery1.SQL.Text);
    UniQuery1.Open;
    max := UniQuery1.FieldByName('max').AsInteger;

    for i2 := min to max do
      begin
        UniQuery1.Close;
        UniQuery1.SQL.Text := 'select 1 from tmov where (numeromov = lpad(' + IntToStr(i2) + ',7,' + QuotedStr('0') + ')) and (codtmv = ' + QuotedStr(vMovTemp[1]) + ') and (dataemissao between ' + QuotedStr(StringReplace(vMovTemp[2],'/','.',[rfReplaceAll,rfIgnoreCase])) + ' and ' + QuotedStr(StringReplace(vMovTemp[3],'/','.',[rfReplaceAll,rfIgnoreCase]))+')';
        //ShowMessage(UniQuery1.SQL.Text);
        UniQuery1.Open;
        if UniQuery1.IsEmpty then
        begin
          Memo2.Lines.Add(vMovTemp[0] + ' ' + vMovTemp[1] + ' ' + IntToStr(i2))
        end;
      end;
  end;
  vMovTemp.Free;


end;

procedure TFrmVerificaSequencial.FormCreate(Sender: TObject);
begin
  vMovs := TStringList.Create;
  vMovs.StrictDelimiter := True;
  vMovs.Delimiter := '|';
end;

procedure TFrmVerificaSequencial.JvEnterAsTab1HandleEnter(Sender: TObject;
  AControl: TWinControl; var Handled: Boolean);
begin
  if TEdit(AControl).Name = 'EdtCodTMV' then
  begin
    if (EdtSerie.Text <> '') and (EdtCodTMV.Text <> '') then
    begin
      BtnAdicionar.Click;
      EdtSerie.SetFocus;
      Handled := True;
    end
    else
    if (EdtSerie.Text <> '') xor (EdtCodTMV.Text <> '') then
    begin
      EdtSerie.Text := '';
      EdtCodTMV.Text := '';
      EdtSerie.SetFocus;
      Handled := True;
    end;


  end;

end;

procedure TFrmVerificaSequencial.Limpar1Click(Sender: TObject);
begin
  if PopupMenu1.PopupComponent.Name = 'Memo1' then
  begin
    vMovs.Clear;
    Memo1.Clear;
  end
  else
  if PopupMenu1.PopupComponent.Name = 'Memo2' then
  begin
    Memo2.Clear;
  end;
end;

end.
